/*
 * PlotPanel.java
 *
 * Created on 29.10.2011, 20:22:12
 */
package eurobot.plot;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;

/**
 *
 * @author Kotuc
 */
public class PlotPanel extends javax.swing.JPanel {

    Graph graph;

    public PlotPanel(Graph graph) {
        this.graph = graph;
        setPreferredSize(new Dimension(1000, 300));
        initComponents();
    }

    @Override
    public void paint(Graphics g) {
//        super.paint(g);

        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.translate(0, 200);
        g2.scale(1, -0.1);
        g2.setColor(Color.black);
        g2.drawLine(0, 0, 1000, 0);
        graph.paint(g2);



    }

    @Override
    public void update(Graphics g) {
        // do not clear
        paint(g);
    }

    public void showInFrame() {
        JFrame frame = new JFrame("Graph");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(this);

        frame.pack();
        frame.setVisible(true);
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                repaint();
            }

        }, 100, 50);
    }

    public static void main(String[] args) throws IOException {

        final Graph graph1 = new Graph();
        graph1.readCSV(new File("./data1.csv"));
        final PlotPanel plotPanel = new PlotPanel(graph1);
        plotPanel.showInFrame();


//        new Thread() {
//
//            @Override
//            public void run() {
//                
//                double time = 0; 
//                while (true) {
//                    time += 1;//System.currentTimeMillis()
//                    graph1.newData(time, 100*Math.sin(time/10), Math.random()*100, 50);
//                    plotPanel.repaint();
//                    try {
//                        Thread.sleep(100);
//                    } catch (InterruptedException ex) {
//                        Logger.getLogger(PlotPanel.class.getName()).log(Level.SEVERE, null, ex);
//                    }
//                    System.out.println("Saving ...");
//                    try {
//                        graph1.writeCSV(new File("./data1.csv"));
//                    } catch (IOException ex) {
//                        Logger.getLogger(PlotPanel.class.getName()).log(Level.SEVERE, null, ex);
//                    }
//                }
//                
//            }
//        }.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
